//
//  MyGroupViewController.m
//  Utils
//
//  Created by enghou on 2018/6/12.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "MyGroupViewController.h"
#import "UILabel+CaculateSize.h"
#import "NetWorkerManager.h"
@interface MyGroupViewController ()

@end

@implementation MyGroupViewController

-(NSArray <SmallGroupItemModel *> *)selectedGroups{
    NSArray *indexpaths = [self.tableView indexPathsForSelectedRows];
    NSMutableArray *targets = [NSMutableArray array];
    for (NSIndexPath *index in indexpaths) {
        BigGroupItemModel *bigModel = self.source[index.section];
        SmallGroupItemModel *smallModel = bigModel.rows[index.row];
        [targets addObject:smallModel];
    }
    return targets;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.editing = YES;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self beginRefresh];
}

-(void)beginRefresh{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/outbox/getInsideGroup.action" parameter:nil completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:[BigGroupItemModel class]];
        if (model.success) {
            self.source = [model.list mutableCopy];
            [self.tableView reloadData];
        }else{
            [self alertSome:kNetWorkFail];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.source.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    BigGroupItemModel *model = self.source[section];
    return model.rows.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    BigGroupItemModel *model = self.source[indexPath.section];
    SmallGroupItemModel *smallModel = model.rows[indexPath.row];
    cell.textLabel.text = smallModel.name;
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]init];
    header.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]init];
    BigGroupItemModel *model = self.source[section];
    label.text = model.orgType;
    CGSize size = [label textSize];
    label.frame = CGRectMake(15, 22 - size.height / 2, size.width, size.height);
    [header addSubview:label];
    return header;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
